import AssemblerBase from "../../Common/Assemblers/AssemblerBase";

const gfx = cc['gfx'];

let vfmtPosUvColor = new gfx.VertexFormat([
    { name: gfx.ATTR_POSITION, type: gfx.ATTR_TYPE_FLOAT32, num: 2 },
    { name: gfx.ATTR_UV0, type: gfx.ATTR_TYPE_FLOAT32, num: 2 },
    { name: gfx.ATTR_COLOR, type: gfx.ATTR_TYPE_UINT8, num: 4, normalize: true },
]);

export default class TextureDisperseAssembler extends AssemblerBase {

    public updateRenderData(comp: cc.RenderComponent) {
        if (comp._vertsDirty) {
            this.updateUVVerts(comp);
            comp._vertsDirty = false;
        }
    }

    /**
     * @override
     * @dec 初始化 `_renderData` 数据
     */
    init(comp: any) {
        super.init(comp);

        this.updatePoint(comp);

        this._renderData = new cc.RenderData();
        this._renderData.init(this);
        this.initData();
    }

    /** 更新自定义顶点数据 */
    updatePoint(comp: any) {
        let xCount = comp.vertXCount;
        if (xCount < 2) { xCount = 2; }

        let yCount = comp.vertYCount;
        if (yCount < 2) { yCount = 2; }

        let countX = xCount - 1;
        let countY = yCount - 1;

        let segmentCount = countX * countY;
        this.verticesCount = 4 * segmentCount;
        this.indicesCount = 6 * segmentCount;
    }

    getVfmt() {
        return vfmtPosUvColor;
    }

    updateUVVerts(comp: any) {
        let pointXNum: number = comp.vertXCount;
        let pointYNum: number = comp.vertYCount;

        if (pointXNum < 2) { pointXNum = 2; }

        if (pointYNum < 2) { pointYNum = 2; }

        /** vdata offset info (default = 2) */
        let uvOffset = this.uvOffset;

        /** vfmtPosUvColor 结构占5个float32 */
        let floatsPerVert = this.floatsPerVert;

        /** 
         * @dec renderData.vDatas 用来存储 pos, uv, color 数据 
         * @dec pos.x, pos.y, uv.x, uv.y, color
        */
        let verts = this._renderData.vDatas[0];

        let node = comp.node;
        let cw = node.width;
        let ch = node.height;

        /** 左下角X坐标 */
        let posX = - cw * node.anchorX;

        /** 左下角Y坐标 */
        let posY = - ch * node.anchorY;

        /** x 坐标偏移 */
        let posXOffset = cw / (pointXNum - 1);

        /** y 坐标偏移 */
        let posYOffset = ch / (pointYNum - 1);

        /** uv x 轴偏移 */
        let uvvXOffset = 1 / (pointXNum - 1);

        /** uv y 轴偏移 */
        let uvvYOffset = 1 / (pointYNum - 1);


        let countX = pointXNum - 1;
        let countY = pointYNum - 1;
        for (let i = 0; i < countX * countY; i++) {
            var row = Math.floor(i / countX);
            var col = Math.floor(i % countX);

            let dstOffset = floatsPerVert * i * 4;

            let ox: number = 0;
            let oy: number = 0;
            let constValue = 130;
            let x = col;
            let y = row;

            // 行和Y对比
            // 列和X对比

            // let midCol = Math.floor(countX / 2);
            // if (col < midCol) {
            //     ox = -constValue - col * 30;
            // }
            // else if (col == midCol) {
            //     ox = 0;
            // }
            // else {
            //     ox = constValue - (col - (midCol + 1)) * 30;
            // }

            // let midRow = Math.floor(countY / 2);
            // if (row < midRow) {
            //     oy = constValue - row * 30;
            // }
            // else if (row == midRow) {
            //     oy = constValue;
            // }
            // else {
            //     oy = -constValue - (row - midRow) * 30;
            // }
            // if (i == 0) { cc.log('i', i, 'ox', ox, 'oy', oy); }
            // cc.log('i', i, 'ox', ox, 'oy', oy);

            let offset = new cc.Vec2(ox, oy);

            // offset = offset.dot(cc.Vec2(2, 1));
            // let offset = new cc.Vec2(0, 0);

            // left bottom
            verts[dstOffset] = posX + col * posXOffset + offset.x;
            verts[dstOffset + 1] = posY + ch - (row + 1) * posYOffset + offset.y;
            verts[dstOffset + 2] = col * uvvXOffset;
            verts[dstOffset + 3] = (row + 1) * uvvYOffset;
            dstOffset += floatsPerVert;

            // right bottom
            verts[dstOffset] = posX + (col + 1) * posXOffset + offset.x;
            verts[dstOffset + 1] = posY + ch - (row + 1) * posYOffset + offset.y;
            verts[dstOffset + 2] = (col + 1) * uvvXOffset;
            verts[dstOffset + 3] = (row + 1) * uvvYOffset;
            dstOffset += floatsPerVert;

            // left top
            verts[dstOffset] = posX + col * posXOffset + offset.x;
            verts[dstOffset + 1] = posY + ch - row * posYOffset + offset.y;
            verts[dstOffset + 2] = col * uvvXOffset;
            verts[dstOffset + 3] = row * uvvYOffset;
            dstOffset += floatsPerVert;

            // right top
            verts[dstOffset] = posX + (col + 1) * posXOffset + offset.x;
            verts[dstOffset + 1] = posY + ch - row * posYOffset + offset.y;
            verts[dstOffset + 2] = (col + 1) * uvvXOffset;
            verts[dstOffset + 3] = row * uvvYOffset;
        }
    }
}